package chapter11;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultiListener {
    Frame frame = new Frame("测试");
    private TextArea ta = new TextArea(6, 40);
    private Button b1 = new Button("按钮一");
    private Button b2 = new Button("按钮二");

    public void init() {
        FirstListener fl = new FirstListener();
        //给b1添加两个监听器
        b1.addActionListener(fl);
        b1.addActionListener(new SecondListener());
        //b2添加一个监听器
        b2.addActionListener(fl);
        frame.add(ta);
        Panel p = new Panel();
        p.add(b1);
        p.add(b2);
        frame.add(p, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    class FirstListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ta.append("第一个监听器被触发，事件源是:" + e.getActionCommand() + "\n");
        }
    }

    class SecondListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ta.append("单击了:" + e.getActionCommand() + "按钮\n");
        }
    }

    public static void main(String[] args) {
        new MultiListener().init();
    }

}
